/********************************************************************************
* Description: used to wakeup eventloop from epoll_wait status
* Author: hsqiang
* Create Date: 2023-06-19
********************************************************************************/
#ifndef AWAKENER_HPP_
#define AWAKENER_HPP_

#include "channel.hpp"
#include "assert_define.hpp"
#include <sys/eventfd.h>
#include <unistd.h>

namespace event_framework
{
    class EventLoop;
    class Awakener final
    {
    public:
        Awakener(EventLoop& loop_) : loop(loop_) {}
        ~Awakener() { Fini(); }
        bool Init();
        void Wakeup();
    private:
        void Fini()
        {
            ASSERT(wakeupChannel);
            ASSERT(wakeupChannel->Fd() > 0);
            close(wakeupChannel->Fd());
        }
        void HandleWakeupEvent();
        
        EventLoop& loop;
        ChannelPtr wakeupChannel;
    };
}

#endif